import Vue from "vue";
import {LANGUAGE_CODE} from "../mutation-types";
import LocaleUtil from "../../utils/LocaleUtil";

const locale = {
  state: {
    data: {},
    language: bootConfig[process.env.NODE_ENV].VUE_APP_LOCALE || 'zh_CN',
  },
  mutations: {
    SET_LOCALE: (state, data) => {
      state.data = data
    },
    SET_LANGUAGE: (state, language) => {
      // 如果禁用了切换语言，则不会更改系统默认语言
      if (language && bootConfig[process.env.NODE_ENV].VUE_APP_LOCALE_ENABLE) {
        state.language = language
      }
      // 初始化时，设置一个默认值，为流程等其他组件提供语言
      Vue.ls.set(LANGUAGE_CODE, state.language)
    },
  },
  actions: {
    setLanguage({commit}, {language, i18n}) {
      commit('SET_LANGUAGE', language)
      LocaleUtil.refreshLocale()
      i18n.locale = this.getters.language
    }
  }
}

export default locale
